#include "StdAfx.h"

void RSI::Initialize(vector<Quotation> stockQuotations)
{
	double gain=0, loss=0, diff=0, avgGain=0, avgLoss=0;
	int i, per = this->_period-1;;

	this->_values.push_back(EMPTY);

	//first value
	for(i=1; i < this->_period; i++) {
		diff = stockQuotations[i].Prices[this->_price] - stockQuotations[i-1].Prices[this->_price];

		if(diff > 0) {
			gain = diff;
			loss = 0;
		}
		else {
			loss = -diff;
			gain = 0;
		}

		avgGain = (per * avgGain + gain) / this->_period;
		avgLoss = (per * avgLoss + loss) / this->_period;
		
		this->_values.push_back(EMPTY);
	}

	this->_values.push_back( 100.0 * (avgGain/(avgGain + avgLoss)) );

	while (i < (int)stockQuotations.size()) 
	{
		diff = stockQuotations[i].Prices[this->_price] - stockQuotations[i-1].Prices[this->_price];
		
		if(diff >= 0) {
			gain = diff;
			loss = 0;
		}
		else {
			loss = -diff;
			gain = 0;
		}

		avgGain = (per * avgGain + gain) / this->_period;
		avgLoss = (per * avgLoss + loss) / this->_period;

		this->_values.push_back( 100.0 * (avgGain/(avgGain + avgLoss)) );
		i++;
	}
}